<style lang="less">
.sett-box{
  height:72px;
}
</style>
<template>
<div class="page-set" id="pageSet">
  <div class="loader-base" v-if="showLoad"><div class="loader-wrap"></div></div>
  <div v-for="(item , index) in todolist" :id="'box'+index">
    <div class="sett-box">
      <div class="sett-top displayflex" :class="{'fixed':index=='0' , 'fixed2':index=='1'&&scrolltop}" >
        <dl class="flex1">
          <dt class="ft17">{{item.Start | getDate}} 至 {{item.End | getDate}}</dt>
          <dd class="ft16 gray">
            <span v-if="usertype == 'Cargowner' || usertype == 'warehouse'">支出</span>
            <span v-if="usertype == 'Driver'">收入</span>
            ￥{{item.WeekMoney}}</dd>
        </dl>
        <div class="icon" @click="showDate" v-if="index=='0'"><i class="iconfont icon-datarange"></i></div>
      </div>
    </div>
    <div class="list-warp">
      <div class="list-ctx lineb" v-for="subItem in item.Main">
        <div class="grab-wrap" @click="goToDetail(subItem.BBillGuid)">
          <div class="addr ft17"><span class="dotted"></span>{{subItem.DepartureProvince | province}}{{subItem.DepartureCity | city}}{{subItem.DepartureDistrict | district}}{{subItem.DepartureAddress}}</div>
          <div class="addr ft17" v-if="subItem.StartBusTerminal"><span class="iconfont icon-trans"></span>{{subItem.StartBusTerminal.replace(/\//g, "")}}</div>
          <div class="addr ft17"><span class="dotted org"></span>{{subItem.ArrivalProvince | province}}{{subItem.ArrivalCity | city}}{{subItem.ArrivalDistrict | district}}{{subItem.ArrivalAddress}}</div>
          <div class="statis ft14 displayflex">
            <span>{{subItem.OrdersVolumn}}m³，{{subItem.OrdersWeight}}kg，共{{subItem.OrdersPKGS}}件</span>
            <div class="flex1 txt-rit"  v-if="usertype == 'Driver'">订单收入：<em class="org">￥</em><span class="price org ft16">{{(subItem.DistributionIncome + subItem.BusTicketAmount)}}</span></div>
            <div class="flex1 txt-rit" v-if="usertype == 'Cargowner' || usertype == 'warehouse'">订单支出：<em class="org">￥</em><span class="price org ft16">{{subItem.CarrierAmount}}</span></div>
          </div>
          <div class="ft14">签收时间：<span class="org">{{subItem.ReceiveTime | ydmhm}}</span></div>
        </div>
      </div>
    </div>
  </div>
  <!-- <div class="noDate_page" v-if="todolist.length<=0">
    <div class="noDate_icon"><i class="iconfont icon-null"></i></div>
    未找到相关数据
  </div>-->
  <div class="noDate_page" v-if="manualShow">
    <div class="noDate_icon"><i class="iconfont icon-null"></i></div>
    未找到相关数据
  </div>
</div>
</template>

<script>
  export default {
    data(){
      return{
        todolist: [],
        dataType:0,
        money:0,
        showLoad:false,
        scrolltop:false,
        manualShow:false,
      }
    },
    computed:{
      dateRange(){
        var _this = this;
        if(_this.$route.query.d1 && _this.$route.query.d2)
        {
          _this.dataType = 2;
          var query = {
            dataType: 2,
            BeginDate: _this.$route.query.d1,
            EndDate: _this.$route.query.d2,
          }
        }
        else 
        {
          _this.dataType = 1;
          var query = {
            dataType: 1,
            BeginDate: _this.getDefaultStartDate(),
            EndDate: _this.getCurrentDate(),
          }
        }
        return query;
      },
      userinfo(){
        if(localStorage.UserInfo){
          return JSON.parse(localStorage.UserInfo)
        };
      },
      usertype(){
        return this.userinfo.UserBizType;
      }
    },
    created(){
      this.getData();
    },
    mounted(){
      window.addEventListener('scroll',this.srcollF)
    },
    methods:{
      srcollF(){
        var t = document.documentElement.scrollTop || document.body.scrollTop; 
        if(!document.getElementById("box0")){
          return false;
        }
        if(t > document.getElementById("box0").offsetHeight){
          this.scrolltop = true;
        }else{
          this.scrolltop = false;
        }
      },
      showDate(){
        this.$router.push({
          path:'/user/showData'
        })
      },
      getData(){
        this.showLoad = true;
        var _this = this,
            query = {},
            api = '';
        _this.dateRange;
        if(_this.usertype == 'Driver') //配送员账号
        {
          query = {
            dataType: _this.dataType,
            DriverGuid: _this.userinfo.StaffID,
            BeginDate : _this.dateRange == null?"":_this.dateRange.BeginDate,
            EndDate : _this.dateRange == null?"":_this.dateRange.EndDate,
            State : 81,
            PageIndex:1,
            PageSize:9999,
          };
          api = 'SearchSettlementsForDriver';
        }

        if(_this.usertype == 'Cargowner') //货主账号
        {
          query = {
            dataType: _this.dataType,
            CorpGuid: _this.userinfo.CorpGuid,
            BeginDate : _this.dateRange == null?"":_this.dateRange.BeginDate,
            EndDate : _this.dateRange == null?"":_this.dateRange.EndDate,
            State : 81,
            PageIndex:1,
            PageSize:9999,
          };
          api = 'SearchSettlementsForCargowner';
        }
         if(_this.usertype == 'warehouse') //仓储账号
        {
          query = {
            dataType: _this.dataType,
            CreateUserId: _this.userinfo.StaffID,
            BeginDate : _this.dateRange == null?"":_this.dateRange.BeginDate,
            EndDate : _this.dateRange == null?"":_this.dateRange.EndDate,
            State : 81,
            PageIndex:1,
            PageSize:9999,
          };
          api = 'SearchSettlementsForCargowner';
        }
        _this.Ajax.post(api,query).then(function(resp){
          _this.showLoad = false;
          if(resp.IsSuccess){
            if(resp.Datas)
            {
              _this.todolist = resp.Datas;
            }
            else
            {
              var virtualItem = {
                Start: _this.dateRange == null?"":_this.dateRange.BeginDate,
                End: _this.dateRange == null?"":_this.dateRange.EndDate,
                WeekMoney: 0,
                Main:[],
              };
              _this.todolist.push(virtualItem);
              _this.manualShow = true;
            }
          } else {
            _this.$vux.toast.show({
              type:'warn',
              text:'数据统计失败，请刷新重试'
            });
          }
        });

      },
      getCurrentDate(){
        var date = new Date();
        var spl = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + spl + month + spl + strDate;
        return currentdate;
      },

      getDefaultStartDate(){
        var week = new Date().getDay();
        if(week == 0) week = 7;
        var sd = new Date();
        sd.setDate(sd.getDate() - (week -1) - 7);
        return sd.getFullYear()+'-'+(sd.getMonth()+1)+'-'+sd.getDate(); 
      },
      goToDetail(guid){
        this.$router.push({
          path: "/orderDetail",
          query: { id: guid }
        });
      }
    }
  }
</script>